package pl.edu.agh.iosr.gamblingzone.model;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;

/**
 * The Class Discipline.
 */
@AttributeOverride(name="id",
		column=@Column(name="DISCIPLINE_ID"))
@Entity(name="DISCIPLINES")
@Inheritance(strategy = InheritanceType.JOINED)
public class Discipline extends EntityModel {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 5392649365004687338L;

	/** The name. */
	@Column(name="DISCIPLINE_NAME", nullable=false)
	private String name;
	
	/** The description. */
	@Column(name="DISCIPLINE_DESCRIPTION", nullable=false)
	private String description;
	
	/** The groups. */
	@OneToMany(mappedBy="discipline", cascade=CascadeType.ALL)
	private List<Group> groups = new LinkedList<Group>();

	/**
	 * Gets the name.
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name.
	 * 
	 * @param name the new name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Gets the description.
	 * 
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Sets the description.
	 * 
	 * @param description the new description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Gets the groups.
	 * 
	 * @return the groups
	 */
	public List<Group> getGroups() {
		return groups;
	}
	
	/**
	 * Adds the group.
	 * 
	 * Automatically set bidirectional association between discipline and group.
	 * Due to cascading mechanism group is automatically made persistent.
	 * 
	 * @param g the g
	 */
	public void addGroup(Group g) {
		g.setDiscipline(this);
		this.groups.add(g);
	}
	
	/**
	 * Removes the group.
	 * 
	 * @param g the g
	 */
	public void removeGroup(Group g) {
		this.groups.remove(g);
		g.setDiscipline(null);
	}
	
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
	// TODO
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Discipline)) return false;

        final Discipline discipline = (Discipline) o;

        if (!name.equals(discipline.name)) return false;
        if (!description.equals(discipline.description)) return false;

        return true;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    // TODO
    public int hashCode() {
    	int result = 17;
    	result = 37 * result + getName().hashCode();
    	result = 37 * result + getDescription().hashCode();
    	
    	return result;
    }
}
